package com.lv.phuotbacnam.ui.fragment;

import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.facebook.ParseFacebookHelper;
import com.lv.phuotbacnam.ui.Section;
import com.lv.phuotbacnam.ui.activity.MainActivity;
import com.lv.phuotbacnam.ui.adapter.ResourcePicsAdapter;
import com.lv.phuotbacnam.ui.navigation.NavigationController;
import com.parse.ParseFacebookUtils;
import com.viewpagerindicator.CirclePageIndicator;

/**
 * Login screen
 */
public class StartFragment extends BaseFragment {

	/* The list of images to show before login with facebook */
	private List<Integer> imgViewPagerRes = Arrays.asList(R.drawable.p0,
			R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
			R.drawable.p5);

	private ViewPager partiesPager;
	private CirclePageIndicator indicator;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_start, container,
				false);

		partiesPager = (ViewPager) rootView
				.findViewById(R.id.fragment_start_viewpager);
		partiesPager.setAdapter(new ResourcePicsAdapter(imgViewPagerRes));

		indicator = (CirclePageIndicator) rootView
				.findViewById(R.id.start_app_pager_indicator);
		indicator.setViewPager(partiesPager);
		indicator.setSnap(true);

		rootView.findViewById(R.id.start_register_facebook).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						doFacebookLogin();
					}
				});

		animationForView();

		return rootView;
	}

	/**
	 * Set animation for view
	 */
	private void animationForView() {
		Animation toBottom = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		toBottom.setDuration(1000);
		toBottom.setInterpolator(new BounceInterpolator());
		partiesPager.startAnimation(toBottom);

	}

	/**
	 * To login with facebook
	 */
	private void doFacebookLogin() {
		final NavigationController navigationController = getNavigationController();
		navigationController.showProgress();

		new ParseFacebookHelper().logInFbWithParse(getActivity(),
				new CallBackFromParse<Object, Object>() {
					@Override
					public void done(Object request, Object result) {
						navigationController.hideProgress();
                        navigationController.navigateTo(MainActivity.class, true, Section.INVITE, null);
                        navigationController.exit();
					}
				});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
	}
}